import { useAuthStore, Message } from "@/store";
import request from "@/utils/requests";

interface IConversationRequest {
  conversationName: string;
  conversationId: string;
  questions: string;
  userId: string;
  appUser: string | null;
}

export interface IConversation {
  id: string;
  userId: string;
  userType: string;
  conversationName: string;
  createTime: string;
  createUserId: string;
  createUsername: string;
  charCount: number,
  [index: string]: any
}

interface IMoveConversationRequest {
  conversationId: string;
  downConversionId: string | null;
}
export async function getConversationList(
  userId: string,
): Promise<IConversation[]> {
  return request<IConversation[]>(`/openai/getConversation/${userId}`, {
    method: "GET",
  });
}
export async function getConversationHistory(
  query: Pick<IConversationRequest, "userId" | "conversationId">,
): Promise<Message[]> {
  return request<Message[]>(`/openai/getChat`, {
    method: "POST",
    body: query,
  });
}
export async function addConversation(
  query: Pick<IConversationRequest, "userId" | "appUser">,
): Promise<IConversation> {
  return request<IConversation>(`/openai/addConversation`, {
    method: "POST",
    body: query,
  });
}
export async function collectConver(
  conversationId: string
): Promise<any> {
  return request<any>(`/openai/collectConver/${conversationId}`, {
    method: "GET",
  });
}
export async function cancelCollectConver(
  conversationId: string
): Promise<any> {
  return request<any>(`/openai/cancelFavoriteConver/${conversationId}`, {
    method: "GET",
  });
}

export async function deleteNotCollectConver(
  userId: string
): Promise<any> {
  return request<any>(`/openai/deleteNotCollectConver/${userId}`, {
    method: "GET",
  });
}

export async function updateConversation(
  query: Pick<IConversationRequest, "conversationId" | "questions" | "userId">,
): Promise<any> {
  return request<any>(`/openai/updateConversation`, {
    method: "POST",
    body: query,
  });
}
export async function updateConversationName(
  query: Pick<IConversationRequest, "conversationId" | "conversationName">,
): Promise<string> {
  return request<string>(`/openai/updateConvName`, {
    method: "POST",
    body: query,
  });
}

export async function deleteConversation(
  query: Pick<IConversationRequest, "userId" | "conversationId">,
): Promise<any> {
  return request<any>(`/openai/deleteConversation`, {
    method: "POST",
    body: query,
  });
}
export async function moveConversation(
  query: IMoveConversationRequest,
): Promise<any> {
  return request<any>(`/openai/moveConversation`, {
    method: "PUT",
    body: query,
  });
}


export async function requestWithPrompt(
  conversationId: string,
  prompt: string,
) {
  const authStore = useAuthStore.getState();
  return updateConversation({
    conversationId,
    questions: prompt,
    userId: authStore.user.id,
  });
}
